#include "SatTime.hpp"
#include <iostream>
#include <sstream>
#include <iomanip>
#include <ctime>


time_t toTime_t(SatTime satTime)
{
    std::tm t = {};
    t.tm_year = satTime.year - 1900;
    t.tm_mon =satTime. month - 1;
    t.tm_mday = satTime.day;
    t.tm_hour = satTime.hour;
    t.tm_min = satTime.minute;
    t.tm_sec = static_cast<int>(satTime.second);
    return std::mktime(&t);
}

double SatTime::operator-(const SatTime &other) const {
    time_t t1 = toTime_t(*this);
    time_t t2 = toTime_t(other);
    double frac_diff = this->second - static_cast<int>(this->second)
                       - (other.second - static_cast<int>(other.second));
    return std::difftime(t1, t2) + frac_diff;
}

std::string SatTime::toString()
{
    std::ostringstream oss;
    oss << std::setfill('0') << std::setw(4) << year << "_"
        << std::setw(2) << month << "_"
        << std::setw(2) << day << "_"
        << std::setw(2) << hour << "_"
        << std::setw(2) << minute << "_"
        << std::setw(2) << static_cast<int>(second);
    return oss.str();
}

